//引入组件库
var nf = sm("do_Notification");
var root = ui("$");
var do_App = sm("do_App");
var do_Page = sm("do_Page");
var hashdata = mm("do_HashData");
var communityUrl = require("url/community");
var communityUtil = require("helper/community");
var qqHttp = require("helper/http");
var listdata = mm("do_ListData");
var noData = [{"template":1,"labelContent":"找不到您搜索的内容纪录"}];
// 本页面-ajax业务请求url
var httpUrl = communityUrl.url.message.page;

// UI
var headerAout = ui("headerAout");
var listView = ui("listView");

listView.bindItems(listdata);

// 定义变量
var page = 1;

// 加载头部
var addheader = headerAout.add("header", "source://view/reuse/searchHeader.ui",
		0, 0);
var textFieldSearch = ui(addheader + ".textFieldSearch");
var type;
var typeName;
// 页面装载完成后，开始初始化工作
do_Page.on("loaded", function() {
	// 读取当前页面的传入参数
	var data = do_Page.getData();
	type = data.type;
	typeName=data.typeName;
	hashdata.addData({
		"searchHint" : "搜索"+typeName+"内容"
	})
	header.refreshData();
	laodPage();
});
// andriod返回键
do_Page.on("back", function() {
	do_App.closePage();
});
// 左上角-返回
var headerLeft = ui(addheader + ".headerLeft");
var header = ui("header");
header.bindData(hashdata);
headerLeft.on("touch", function() {
	do_App.closePage()
});
var search;
textFieldSearch.on("enter", function(data, e) {
	sendSearch();
});
var sendBtn = ui(addheader + ".sendBtn");
sendBtn.on("touch", function(data, e) {
	sendSearch();
});
function sendSearch(){
	do_Page.hideKeyboard();
	search = textFieldSearch.text;
	if(null!=search&&''!=search){
		laodPage();
	}
	else{
		nf.toast("请输入关键字");
	}
}
function laodPage() {
	page = 1;
	var params = {
		communityId : communityUtil.communityVisitId(),
		page : page,
		search : search,
		type : type
	};
	qqHttp.post(httpUrl, params, function(data) {
		listView.rebound();
		listdata.removeAll();
		if(data.page.content.length){
			listdata.addData(data.page.content);
		}
		else{
			listdata.addData(noData);
		}
		listView.refreshItems();
	})
}
// 刷新数据
function getNextPageData() {
	page++;
	var params = {
		communityId : communityUtil.communityVisitId(),
		page : page,
		search : search,
		type : type
	};
	qqHttp.post(httpUrl, params, function(data) {
		listView.rebound();
		listdata.addData(data.page.content);
		listView.refreshItems();
	})
}

// 下拉列表，刷新数据
listView.on("pull", function(data) {
	if (data.state == 2) {
		laodPage();
	}
});

// 上拉列表，翻页数据
listView.on("push", function(data) {
	if (data.state == 2) {
		getNextPageData();
	}
});